package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.ExamResponse;
import com.alpha.bom.Professor;
import com.alpha.bom.Student;
import com.alpha.bom.User;
import com.alpha.dao.ExamResponseDao;
import com.alpha.dao.StudentDao;

/**
 * Servlet implementation class MyExamsResultsServlet
 */
@WebServlet("/ExamineeReportServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin","user"}))
public class ExamineeReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(ExamineeReportServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExamineeReportServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		User user = null; 
		HttpSession session = request.getSession();
		ArrayList<ExamResponse> exam_responses = new ArrayList<ExamResponse>();
		
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Student)){
			user = (Student)session.getAttribute("user");
			ExamResponseDao rDao = new ExamResponseDao();
			try {
				for(ExamResponse eRes: rDao.retrieveExamResponseByStudent(user.getUserId())){
					if(eRes.isComplete()){
						exam_responses.add(eRes);
					}
				}
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			request.setAttribute("box_title", "Exam Results");
			request.setAttribute("exam_responses", exam_responses);
			RequestDispatcher dis=request.getRequestDispatcher("ExamineeReport.jsp");
            dis.forward(request, response);
            return;
		} else if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Professor)){
			user = (Professor)session.getAttribute("user");
			if(request.getParameter("student_id")!=null){
				int studentId = Integer.parseInt(request.getParameter("student_id"));
				Student student=  null;
				ExamResponseDao rDao = new ExamResponseDao();
				StudentDao sDao = new StudentDao();
				try {
					student = sDao.retrieveStudent(studentId);
					exam_responses = rDao.retrieveExamResponseByStudent(student.getUserId());
				} catch (NamingException e) {
					logger.error(e.getMessage());
					return;
				} catch (SQLException e) {
					logger.error(e.getMessage());
					return;
				}

				request.setAttribute("box_title", student.getLastName()+", "+student.getFirstName());			
				request.setAttribute("exam_responses", exam_responses);
				RequestDispatcher dis=request.getRequestDispatcher("ExamineeReport.jsp");
	            dis.forward(request, response);
	            return;
			} else {
				logger.error("Student not found.");
				return;
			}
		} else {
			logger.error("User not found.");
			response.sendRedirect("Login.jsp?message=User+not+found.");
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
